package com.open.capacity.reimburse.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.open.capacity.reimburse.excel.BudgetExcelModel;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.PbdBudget;
import com.open.capacity.reimburse.dao.PbdBudgetDao;
import com.open.capacity.reimburse.service.PbdBudgetService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class PbdBudgetServiceImpl implements PbdBudgetService {

    @Autowired
    private PbdBudgetDao pbdBudgetDao;

    /**
     * 添加
     *
     * @param pbdBudget
     */
    public int save(PbdBudget pbdBudget) {
        pbdBudget.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        pbdBudget.setUpdateTime(new Date());
        return pbdBudgetDao.save(pbdBudget);
    }

    /**
     * 修改
     *
     * @param pbdBudget
     */
    public int update(PbdBudget pbdBudget) {
        pbdBudget.setUpdateTime(new Date());
        return pbdBudgetDao.update(pbdBudget);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return pbdBudgetDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public PbdBudget findById(String id) {
        return pbdBudgetDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<PbdBudget> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<PbdBudget> list = pbdBudgetDao.findAll(params);
        PageInfo<PbdBudget> pageInfo = new PageInfo(list);

        return PageResult.<PbdBudget>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<PbdBudget> findAll(Map<String, Object> params) {
        return pbdBudgetDao.findAll(params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int importExcel(MultipartFile multipartFile) throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        // params.setTitleRows(0);
        // 开启excel校验
        params.setNeedVerify(true);
        params.setStartRows(2);

        ExcelImportResult<BudgetExcelModel> result = new ExcelImportResult();
        try {
            result = ExcelImportUtil.importExcelMore(multipartFile.getInputStream(),
                    BudgetExcelModel.class, params);
        } catch (Exception e) {
            log.error("从文件读取数据错误:{}", e.getMessage());
        }
        if (result.isVerifyFail()) {
            StringBuilder temp = new StringBuilder();
            for (BudgetExcelModel entity : result.getFailList()) {
                temp.append("第").append(entity.getRowNum()).append("行的错误是：").append(entity.getErrorMsg());
            }
            throw new Exception("Excel文件校验不通过:" + temp.toString());
        }

        for (BudgetExcelModel item : result.getList()) {
            PbdBudget pbdBudget = new PbdBudget();
            BeanUtils.copyProperties(item, pbdBudget);
            save(pbdBudget);
        }
        return result.getList().size();
    }

}
